var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
const bgMusicAudioContext = wx.getBackgroundAudioManager()
Page({
  data: {
    curSlideIndex: 0,
    isShowVideo: false,
    videoShow: false,
    isShowModal: false,
    buyNumber: 1,
    detailInfo:{}
  },
  onLoad: function (e) {
    var that = this;
    that.setData({
      id:e.id
    })
    that.requestDetail();
  },
  onShow: function () {
    app.setNavtitle('积分商城');
    var that = this;
    // if (wx.getStorageSync('jfGoodaddress')){
      wx.getStorage({
        key: 'jfGoodaddress',
        success: function (res) {
          console.log(res.data);
          var curAddress = res.data;
          that.setData({
            curAddress: curAddress
          })
          that.changepostmoney(curAddress);
        },
        fail(){
          console.log('没有缓存');
          that.setData({
            curAddress: ''
          })
        }
      })
    // }else{
    //   that.setData({
    //     curAddress: ''
    //   })
    // }
  },
  onReady: function () {
    var that = this;
    that.videoContext = wx.createVideoContext('myVideo');
  },
  onHide: function () {
    this.hideVideo();
  },
  //播放视频
  playVideo: function (e) {
    var that = this;
    if (app.globalData.isPlay) {
      // app.toggleInfoPlay(that);
      bgMusicAudioContext.stop();
      app.globalData.curInfoMusicid = '';
      app.globalData.isPlay = false;
      app.globalData.isPlaynew = 'new';
    }
    that.setData({
      isShowVideo: true
    });
    that.videoContext.play();
  },
  // 关闭视频
  hideVideo: function () {
    var that = this;
    that.setData({
      isShowVideo: false
    })
  },
  // 请求商品详情
  requestDetail: function () {
    var that = this;
    var id = that.data.id;
    console.log(id + "--------");
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_community_goods_detail',
        gid: id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          if (res.data.data.video != '') {
            that.setData({
              video: res.data.data.video,
              videoShow: true,
            })
          }
          that.setData({
            detailInfo: res.data.data,
            postFee: res.data.data.freight
          })
          var article = res.data.data.detail;
          // 富文本解析
          WxParse.wxParse('article', 'html', article, that, 5);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  dotsChange: function (e) {//改变当前幻灯索引
    this.setData({
      curSlideIndex: e.detail.current
    })
  },
  toaddressManage:function(){
    wx.navigateTo({
      url: '/subpages/addressManage/addressManage?type=jfgood',
    })
  },
  nowChange: function () {
    var that = this;
    var curAddress = that.data.curAddress;
    var goods = [];
    var goodsobj = {
      gid: that.data.id,
      num: 1
    };
    goods.push(goodsobj);
    var data = {
      map: 'applet_community_points_trade',
      buys: goods
    }
    if (that.data.detailInfo.type == 1){
      if (curAddress){
        if (curAddress.id) {
          data.addrid = curAddress.id;
          data.postFee = that.data.postFee;
        } else {
          app.errorTip(that, '请选择收货地址', 2000);
          return;
        }
      }else{
        app.errorTip(that, '请选择收货地址', 2000);
        return;
      }
    }
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          console.log(res.data.data)
          var tid = res.data.data.tid;
          if (res.data.data.status == 'zfcg') {
            wx.navigateTo({
              url: '/subpages/dhOrderDetail/dhOrderDetail?id=' + tid
            })
          } else {
            that.orderPay(tid);
          }
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  changepostmoney: function (address) {
    var that = this;
    var goods = [];
    var goodsobj = {
      gid: that.data.id,
      num: 1
    };
    goods.push(goodsobj);
    var prov = address.province;
    var city = address.city;
    console.log(prov);
    console.log(city);
    console.log(goods)
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_get_post_fee',
        prov: prov,
        city: city,
        goods: goods
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.ec == 200) {
          console.log(res.data.data)
          that.setData({
            postFee: res.data.data
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        // wx.hideToast();
      }
    });
  },
  orderPay: function (tid) {
    var that = this;
    var data = {};
    data.map = 'applet_order_pay';
    data.tid = tid;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              wx.showLoading({
                title: '加载中',
              })
              setTimeout(function () {
                wx.hideLoading();
                wx.navigateTo({
                  url: '/subpages/dhOrderDetail/dhOrderDetail?id=' + tid
                })
              }, 1000)
            },
            'fail': function (res) {
              console.log(res);
            }
          });
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  nowJforder: function (){
    var that = this;
    that.setData({
      isShowModal: true
    })
  },
  hideModal: function () {
    var that = this;
    that.setData({
      isShowModal: false
    })
  },
  //单张图片预览
  singlePeiviewImg: function (e) {
    var curimg = e.currentTarget.dataset.curimg;
    var imgs = new Array(e.currentTarget.dataset.imgs);
    wx.previewImage({
      current: curimg, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  changeNum: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var buyNum = parseInt(that.data.buyNumber);
    var stock = that.data.detailInfo.stock;
    if (type == 'add') {
      if (stock == 0) {
        app.errorTip(that, '商品库存不足', 2000);
      }
      if (buyNum < stock) {
        buyNum++;
      } else {
        buyNum = stock;
      }
    } else {
      if (buyNum==1){return;}
      buyNum--;
    }

    that.setData({
      buyNumber: buyNum
    })
  },
  tojforderForm: function () {
    var that = this;
    var orderArr = [];
    var orderObj = {};
    orderObj.gid = that.data.detailInfo.id;
    orderObj.num = that.data.buyNumber;
    var stock = parseInt(that.data.detailInfo.stock);
    orderArr.push(orderObj);
    var data = {};
    data.map = 'applet_order_create';
    data.buys = encodeURI(JSON.stringify(orderArr));
    data.isPoint = 1;
    if (orderObj.num > stock) {
      app.errorTip(that, '商品数量超出库存', 2000);
      return;
    }
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.setStorage({
            key: "submitOrder",
            data: res.data.data,
            success: function (res) {
              console.log(res);
              wx.navigateTo({
                url: '/subpages/jforderForm/jforderForm'
              })
            }
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})
